# -*- coding:utf-8 -*-
import serial
import time
#设置串口、波特率以及发送数据间隔
ser = serial.Serial("/dev/ttyUSB3",9600,timeout=0.01) 
ser.flushInput()
while True:
    print("You can always receive data, press Ctrl + C to exit")

    printData = bytearray()
    #0xaa
    printData.append(170)
    # #0x55
    printData.append(85)
    #选择模式：手动/自动
    Data1 = int(input("    1---Auto   2---Handle  3--Updata\nyour select:"))
    printData.append(Data1)
    #选择设备
    Data2 = int(input("    1---Fan1   2---Fan2  3--Light  4--Motor\nyour deviceID:"))
    printData.append(Data2)
    #设定数值
    Data3 = int(input("your Value:"))
    printData.append(Data3//10)
    printData.append(Data3%10)
    #设定正反
    Data4 = int(input("    0--Positive  1--Negtive\nyour select:"))
    printData.append(Data4)
    #设定运行时间：只针对步进电机
    Data5 = int(input("your running time:"))
    printData.append(Data5//10)
    printData.append(Data5%10)

    ser.write(printData)
    ser.flush() # 等待所有数据写出
    if ser.inWaiting() > 0: # Get the number of bytes in the input buffer
        data = ser.readline()
        if data != "":
            if data[0]==187 and data[1]==85:
                # 收到状态回复
                print(data)
        ser.flushInput() # 丢弃接收缓存中的所有数据